var Topbar = {
	showCreateAccount: function() {
		jQuery('div#loginDiv').hide();
		jQuery('div#createAccountDiv').fadeIn();
	},
	
	showLogin: function() {
		jQuery('div#createAccountDiv').hide();
		jQuery('div#loginDiv').fadeIn();
	},
	
	validateLogin: function() {
		var email = jQuery("input#loginEmail").val() || "";
		var password = jQuery("input#loginPassword").val() || "";
		var success = true;
		var message = "";
		
		if(email == ""){
			message += "Please enter a valid email.";
			success = false;
		}
		
		if(password == ""){
			if(message.length > 0){
				message += "</br>";
			}
			message += "Please enter a password.";
			success = false;
		}
		
		if(!success){
			System.showValidation(message);
		}
		return success;
	},
	
	validateCreateAccount: function() {
		var form = jQuery("form#createForm");
		var email = form.find('input[name=email]').val() || "";
		var password = form.find('input[name=password]').val() || "";
		var first_name = form.find('input[name=first_name]').val() || "";
		var last_name = form.find('input[name=last_name]').val() || "";
		var success = true;
		
		if(email == ""){
			System.showValidation("Please enter a valid email.");
			success = false;
		}
		
		if(password == ""){
			System.showValidation("Please enter a password.");
			success = false;
		}
		
		if(first_name == ""){
			System.showValidation("Please enter your first name.");
		}
		
		if(last_name == ""){
			System.showValidation("Please enter your last name.");
		}
		return success;
	},
	
	login: function() {
		if(Topbar.validateLogin()){
			var args = jQuery('form#loginForm').serialize()+"&cfc=loginService&method=run&callMethod=login";
			jQuery.ajax({
				url:"/cfcs/remote.cfc?"+args,
				type:"GET",
				success:function(data){
					if(data > 0){
						jQuery(document).trigger('Topbar.login');
						Topbar.reloadLogin();
					} else {
						System.showValidation("Invalid Login.");
					}
				}
			});
		}
	},
	
	createAccount: function() {
		if(Topbar.validateCreateAccount()){
			var args = jQuery('form#createForm').serialize()+"&cfc=userService&method=run&callMethod=createAccount";
			jQuery.ajax({
				url:"/cfcs/remote.cfc?"+args,
				type:"GET",
				success:function(data){
					if(data > 0){
						jQuery(document).trigger('Topbar.login');
						Topbar.reloadLogin();
					}
				}
			});
		}
	},
	
	logout: function() {
		var args = jQuery('form#loginForm').serialize()+"&cfc=loginService&method=run&callMethod=logout";
		jQuery.ajax({
			url:"/cfcs/remote.cfc?"+args,
			type:"GET",
			success:function(data){
				jQuery(document).trigger('Topbar.logout');
				Topbar.reloadLogin();
			}
		});
	},
	
	reloadLogin: function(){
		var args = jQuery('form#loginForm').serialize()+"&cfc=loginUtil&method=run&callMethod=renderLogin";
		jQuery.ajax({
			url:"/cfcs/remote.cfc?"+args,
			type:"GET",
			success:function(data){
				jQuery('div#loginAreaDiv').html(data);
			}
		});
	},
	
	fixTopBar:function(){
		if(Topbar.body.scrollTop() > 0){
			Topbar.topbar.addClass('fixed');
		} else {
			Topbar.topbar.removeClass('fixed');
		}
	}
};

jQuery(document).ready(function(){
	console.log('hi');
	var Topbar.body = jQuery(document);
	var Topbar.topbar = jQuery('div#top_bar');
	console.log(Topbar.body);
	Topbar.body.scroll(function(){
		Topbar.fixTopBar();
		console.log('hi');
	});
});

